package com.pony.controller.admin;

import com.pony.enums.AppHttpCodeEnum;
import com.pony.result.R;
import com.pony.dto.EmployeeLoginDTO;
import com.pony.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 后台-员工管理
 * @auther pony
 * @create 2023-11-16 10:55
 */
@Slf4j
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 验证码接口-以流的形式写回验证码图片
     * @param request
     * @param response
     */
    @GetMapping("/getVerifyCode")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response){
        employeeService.getVerifyCode(request, response);
    }

    /**
     * 登录
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody EmployeeLoginDTO dto, HttpServletRequest request){
        log.info("登录用户信息：" + dto);
        // 参数校验
        if (dto.getUsername()==null||"".equals(dto.getUsername())){
            R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        if (dto.getPassword()==null||"".equals(dto.getPassword())){
            R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        if (dto.getVerifyCode()==null||"".equals(dto.getVerifyCode())){
            R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return employeeService.login(dto, request);
    }

    /**
     * 根据id，查询单个员工信息
     * @return
     */
    @GetMapping
    public R getEmployeeInfoById(){
        return employeeService.getEmployeeInfoById();
    }

    /**
     * 登出接口
     * @return
     */
    @GetMapping("logout")
    public R logout(){
        return employeeService.logout();
    }
}
